# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=mesa
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=22.3.3
pkgrel=1
pkgdesc="Open-source implementation of the OpenGL, Vulkan and OpenCL specifications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python-mako"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-libclc"
             "${MINGW_PACKAGE_PREFIX}-libelf"
             "${MINGW_PACKAGE_PREFIX}-glslang")
depends=("${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-zstd"
         "${MINGW_PACKAGE_PREFIX}-spirv-llvm-translator"
         "${MINGW_PACKAGE_PREFIX}-spirv-tools"
         "${MINGW_PACKAGE_PREFIX}-opencl-icd")
optdepends=("${MINGW_PACKAGE_PREFIX}-opengl-man-pages: for the OpenGL API man pages"
            "${MINGW_PACKAGE_PREFIX}-vulkan-validation-layers: to debug applications using Vulkan graphics")
url="https://www.mesa3d.org/"
license=('MIT')
options=('staticlibs' 'strip')
source=(https://mesa.freedesktop.org/archive/${_realname}-${pkgver}.tar.xz{,.sig}
        llvmwrapgen.sh
        0005-win32-The-opengl-headers-should-install-to-mesa-GL-s.patch
        0000-fix-dzn-build-with-clang.patch)
sha256sums=('bed799788bf2bd9ef079d97cd8e09348bf53cb086818578e40773b2b17812922'
            'SKIP'
            '69f21522f20c10f5699dfe3e128aa88d4fedde816f6e8df1506d7470c44bf3da'
            '7f7874b89103c98dc0006bb64d209b5c0afa5a72b27e5e3d9f366a9609881714'
            '82fd4661500fb245cea50f14c64ee9062c636adfebde4c101c73b777d8ee0759')
validpgpkeys=('8703B6700E7EE06D7A39B8D6EDAE37B02CEB490D') # Emil Velikov <emil.l.velikov@gmail.com>
validpgpkeys+=('946D09B5E4C9845E63075FF1D961C596A7203456') # Andres Gomez <tanty@igalia.com>
validpgpkeys+=('E3E8F480C52ADD73B278EE78E1ECBE07D7D70895') # Juan Antonio Suárez Romero (Igalia, S.L.) <jasuarez@igalia.com>"
validpgpkeys+=('A5CC9FEC93F2F837CB044912336909B6B25FADFA') # Juan A. Suarez Romero <jasuarez@igalia.com>
validpgpkeys+=('71C4B75620BC75708B4BDB254C95FAAB3EB073EC') # Dylan Baker <dylan@pnwbakers.com>
validpgpkeys+=('57551DE15B968F6341C248F68D8E31AFC32428A6') # Eric Engestrom <eric@engestrom.ch>

noextract=(${_realname}-${pkgver}.tar.xz)

case ${MSYSTEM} in
  MINGW32)
    _mach=x86
  ;;
  MINGW64)
    _mach=x86_64
  ;;
  UCRT64)
    _mach=ucrt-x86_64
  ;;
  CLANG32)
    _mach=clang-x86
  ;;
  CLANG64)
    _mach=clang-x86_64
  ;;
esac

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xJf ${srcdir}/${_realname}-${pkgver}.tar.xz -C ${srcdir} || true

  cd ${srcdir}/${_realname}-${pkgver}

  apply_patch_with_msg \
    0005-win32-The-opengl-headers-should-install-to-mesa-GL-s.patch
# Fix dozen build with clang.
# https://gitlab.freedesktop.org/mesa/mesa/-/issues/7885
# https://gitlab.freedesktop.org/mesa/mesa/-/issues/7234
# https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/20676
  apply_patch_with_msg \
    0000-fix-dzn-build-with-clang.patch

# Generate binary wrap for LLVM if llvm-config tool is busted
  if ! [ "$(${MINGW_PREFIX}/bin/llvm-config --has-rtti 2>&1)" = YES ] && ! [ "$(${MINGW_PREFIX}/bin/llvm-config --has-rtti 2>&1)" = NO ]; then
    nollvmconfig=1 llvm_static=false ${srcdir}/llvmwrapgen.sh
  fi
}

build() {
  cd ${srcdir}/${_realname}-${pkgver}
  buildconf="${MINGW_PREFIX}/bin/meson ./build/windows-${_mach}
  --prefix=${MINGW_PREFIX}
  --buildtype=release
  --backend=ninja
  -Dshared-glapi=disabled
  -Degl=disabled
  -Dgles1=disabled
  -Dgles2=disabled
  -Dllvm=enabled
  -Dshared-llvm=enabled
  -Dosmesa=true
  -Dbuild-tests=false
  -Dvulkan-drivers=swrast,amd,microsoft-experimental
  -Dgallium-drivers=swrast,zink,d3d12
  -Dmicrosoft-clc=enabled
  -Dstatic-libclc=all
  -Dcpp_rtti=true
  -Dmin-windows-version=7
  -Dgallium-opencl=icd
  -Dopencl-spirv=true"

# Fallback to binary wrap if llvm-config tool is busted
  if ! [ "$(${MINGW_PREFIX}/bin/llvm-config --has-rtti 2>&1)" = YES ] && ! [ "$(${MINGW_PREFIX}/bin/llvm-config --has-rtti 2>&1)" = NO ]; then
    buildconf="${buildconf}
     --force-fallback-for=llvm"
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  PROCESSOR_ARCHITECTURE="${CARCH}" ${buildconf} \
  -Dc_args='-march=core2 -pipe' \
  -Dcpp_args='-march=core2 -pipe' \
  -Dc_link_args='-s' \
  -Dcpp_link_args='-s'

  ${MINGW_PREFIX}/bin/ninja -C ./build/windows-${_mach}
}

package() {
  cd ${srcdir}/${_realname}-${pkgver}/build/windows-${_mach}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install --skip-subprojects

# fix osmesa version in .pc file
  sed -e "s|8.0.0|${pkgver}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/osmesa.pc

# Fix JSON for RADV
  if compgen -G "${pkgdir}${MINGW_PREFIX}/share/vulkan/icd.d/radeon_icd*.json" > /dev/null; then
    sed -e "s|./vulkan_radeon|${MINGW_PREFIX}/bin/libvulkan_radeon|g" \
      -i $(find ${pkgdir}${MINGW_PREFIX}/share/vulkan/icd.d -name "radeon_icd*.json" -print)
  fi

  # Remove KHR include, fixes conflict with mingw-w64
  rm -rf "${pkgdir}${MINGW_PREFIX}"/include/KHR
}
